package com.mdc.mansion.core.modules.restaurant.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.mdc.mansion.core.common.restaurant.RestaurantCommon;
import com.mdc.mansion.core.modules.system.SysDictService;
import com.mdc.mansion.dataaccess.modules.system.entity.Dict;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mdc.mansion.core.modules.restaurant.RestaurantConfigService;
import com.mdc.mansion.core.modules.restaurant.RestaurantExtService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.common.service.CrudService;
import com.mdc.mansion.dataaccess.modules.restaurant.dao.RestaurantConfigDao;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantConfig;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantExt;

/**
 * 餐厅配置信息ServiceImpl
 * @author mingdongc
 * @version 2018-01-01
 */
@Service
public class RestaurantConfigServiceImpl extends CrudService<RestaurantConfigDao, RestaurantConfig> implements RestaurantConfigService {

    @Resource
    private RestaurantExtService restaurantExtService;
    @Autowired
    private SysDictService sysDictService;
    /**(non-Javadoc)
    * @param restaurantConfig
    * @see com.mdc.mansion.core.modules.restaurant.RestaurantConfigService#saveOrUpdate(com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantConfig)
    */  
    @Transactional
    @Override
    public void saveOrUpdate(RestaurantConfig restaurantConfig) {
        // 先验证是否存在 
        RestaurantConfig param = new RestaurantConfig();
        param.setType(restaurantConfig.getType());
        param.setRestaurantId(ContextHolder.currentRestaurantId());
        param.setPage(new Page<RestaurantConfig>(-1));
        List<RestaurantConfig> oldRestaurantConfigList = this.findList(param);
        if(oldRestaurantConfigList!=null&&oldRestaurantConfigList.size()>0){ // 修改 
            RestaurantConfig oldRestaurantConfig = oldRestaurantConfigList.get(0);
            oldRestaurantConfig.setVal(restaurantConfig.getVal());
            this.updateSelective(oldRestaurantConfig);
        }else{// 新增
            restaurantConfig.setRestaurantId(ContextHolder.currentRestaurantId());
            this.save(restaurantConfig);
        }
    }

    @Override
    public Map<String, String> baseInfo() {
        Map<String, String> map = new HashMap<>();
        String restaurantId = ContextHolder.currentRestaurantId();
        RestaurantConfig paramRestaurantConfig = new RestaurantConfig();
        paramRestaurantConfig.setRestaurantId(restaurantId);
        paramRestaurantConfig.setPage(new Page<RestaurantConfig>(-1));
        List<RestaurantConfig> configList = this.findList(paramRestaurantConfig);
        if(configList!=null && configList.size()>0){
            for(RestaurantConfig restaurantConfig : configList){
                map.put(restaurantConfig.getType(), restaurantConfig.getVal());
            }
        }
        // 获取餐厅的单位信息
        RestaurantExt restaurantExt=new RestaurantExt();
        String id = ContextHolder.currentRestaurant().getId();
        restaurantExt.setRestaurantId(id);
        restaurantExt.setType(RestaurantExt.TYPE.DISHES_UNIT.getVal());
        List<RestaurantExt> restaurantExts = restaurantExtService.findList(restaurantExt);
        JSONArray dishesUnitArray = new JSONArray();
        if(CollectionUtils.isNotEmpty(restaurantExts)){
            for(RestaurantExt ext:restaurantExts){
                String value = ext.getVal();
                JSONObject valueObj = JSONObject.parseObject(value);
                dishesUnitArray.add(valueObj);
            }
         }
        map.put(RestaurantExt.TYPE.DISHES_UNIT.getVal(), dishesUnitArray.toJSONString());
        /***新增轮播图数据读取 sys_dict ***/
        Dict sysDict = new Dict();
        sysDict.setType(RestaurantCommon.Polling_img);
        List<Dict> dictList = sysDictService.findList(sysDict);
        if( !(null == dictList || 0== dictList.size()))
        {
        	 for(Dict dict : dictList){
                 map.put(dict.getType(), dict.getValue());
             }
        }

        return map;
    }
}
